<?php

namespace App\Services;

use App\Models\AdminUser;
use App\Repositories\BookApplyRepository;
use App\Repositories\PatentApplyRepository;
use App\Repositories\PlantVarietyRepository;
use App\Transverse;

class NavbarService
{

    public function todo($user, $role): array
    {
        $data = [];
        if ($item = $this->patentTodo($user, $role)) {
            $data[] = $item;
        }
        if ($item = $this->transverseTodo($role)) {
            $data[] = $item;
        }
        if ($item = $this->softwareTodo($user, $role)) {
            $data[] = $item;
        }
        if ($item = $this->plantVarietiesTodo($user, $role)) {
            $data[] = $item;
        }
        return $data;
    }

    protected function patentTodo(AdminUser $user, $role = 0): array
    {
        if (!config('settings.module.patent_module')) {
            return [];
        }
        $data = [];
        if ($role == 1) {
            $data = [
                'title' => '专利申请前评估',
                'href'  => admin_route('patent.pg.review'),
                'count' => app(PatentApplyRepository::class)->needReviewCount($user, 1)
            ];
        } elseif ($role == 2) {
            $data = [
                'title' => '专利申请前登记审批',
                'href'  => admin_route('patent.applies.index', ['is_review' => 1]),
                'count' => app(PatentApplyRepository::class)->deptReviewCount($user, 0)
            ];
        }
        return $data;
    }

    protected function transverseTodo($role): array
    {
        if ($role != 1 || !config('settings.module.project_module')) {
            return [];
        }
        return [
            'title' => '横向课题审批',
            'href'  => admin_route('transverses.index', ['_scope_' => 'review']),
            'count' => Transverse::query()->where('review_status', 0)->count()
        ];
    }


    protected function softwareTodo(AdminUser $user, $role = 0): array
    {
        if (!in_array($role, [1, 2]) || !config('settings.module.software_module')) {
            return [];
        }
        if ($role == 1) {
            $count = app(BookApplyRepository::class)->needReviewCount($user, 1);
        } else {
            $count = app(BookApplyRepository::class)->deptReviewCount($user, 0);
        }
        return [
            'title' => '软件著作申请前审批',
            'href'  => admin_route('bookApplies.index', ['action' => 'review']),
            'count' => $count
        ];
    }

    protected function plantVarietiesTodo(AdminUser $user, $role = 0): array
    {
        if (!in_array($role, [1, 2]) || !config('settings.module.plant_module')) {
            return [];
        }
        if ($role == 1) {
            $count = app(PlantVarietyRepository::class)->needReviewCount($user, 1);
        } else {
            $count = app(PlantVarietyRepository::class)->deptReviewCount($user, 0);
        }
        return [
            'title' => '植物新品种申请前审批',
            'href'  => admin_route('plantVarieties.index', ['action' => 'review']),
            'count' => $count
        ];
    }
}
